package com.io.nio2;

/**
 * @author hjj
 * @create 2019-11-24 19:19
 */
public class NIOClient {
    private static String DEFAULT_HOST = "127.0.0.1";
    private static int DEFAULT_PORT = 8890;
    private static NIOClientHandler nioClientHandler;

    public static void start() {
        start(DEFAULT_HOST, DEFAULT_PORT);
    }

    public static void stop() {
        nioClientHandler.stop();
    }

    public static synchronized void start(String ip, int port) {
        if (nioClientHandler != null) nioClientHandler.stop();
        nioClientHandler = new NIOClientHandler(DEFAULT_HOST, DEFAULT_PORT);
        new Thread(nioClientHandler, "NIOClientHandler").start();
    }

    public static boolean sendMsg(String msg) throws Exception{
        if ("q".equals(msg)) return false;
        nioClientHandler.send(msg);
        return true;
    }
}
